﻿Imports ComponentFactory.Krypton.Toolkit
Imports System.Data.OleDb
Imports WiseLibraries.Crypto
Imports WiseLibraries.Strings

Public Class ConsultaUsuario
    Inherits KryptonForm
    Dim dr As OleDbDataReader
    Dim cmd As OleDbCommand
    Dim conex As OleDbConnection
    Dim val As New RegexValidator()
    Dim ic As ICrypto = CryptoFactory.makeCryptographer(WiseLibraries.SortingAlgorithm.Rijndael)
#Region "strings"
    Dim strLogin = "SELECT tblNivelUsuario.*, tblLogin.* " & _
                    "FROM tblNivelUsuario INNER JOIN tblLogin ON tblNivelUsuario.codnivelUsuario = tblLogin.codnivelUsuario " & _
                    "WHERE codLogin = " & Login.usuario.Codigo

#End Region
    Sub New()
        ' This call is required by the Windows Form Designer.
        InitializeComponent()
        ' Add any initialization after the InitializeComponent() call.

        conex = FabricaDeConexoes.CriarConexao()
        conex.Open()

        cmd = New OleDbCommand(strLogin, conex)
        Try
            dr = cmd.ExecuteReader()
            dr.Read()
            'carregar campos
            lblusuario.Text = dr("usuarioLog")
            lblnivel.Text = dr("descricaonivelUsuario")
            txtsenha.Text = ic.Decrypt(dr("senhaLog"))
            txtemail.Text = dr("emailUsuario")
        Catch ex As Exception
            Debug.WriteLine(ex)
        Finally
            dr.Close()

        End Try
    End Sub

    Private Sub btneditar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btneditar.Click
        If Not (txtsenha.Enabled = True) Then
            txtsenha.Clear()
            lblsenhat.Text = "Senha:*"
            lblconfirmt.Text = "Confirme a senha:*"
            lblemailt.Text = "Email:*"
            lblaviso.visible = True
            txtconfirmsenha.Enabled = True
            txtsenha.Enabled = True
            txtemail.Enabled = True
        End If

    End Sub

    Private Sub btncancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnfechar.Click
        Me.Dispose()
    End Sub
    Private Sub atualizausuario()
        If (String.IsNullOrEmpty(txtsenha.Text) Or String.IsNullOrEmpty(txtconfirmsenha.Text) Or String.IsNullOrEmpty(txtemail.Text)) Then
            KryptonMessageBox.Show("Preencha todos os campos!", "Atenção")
        Else
            If (txtsenha.Text.Equals(txtconfirmsenha.Text)) Then
                Dim senha = ic.Encrypt(txtsenha.Text)
                If (val.validateEmail(txtemail.Text)) Then
                    Dim email As String = txtemail.Text
                    Dim strupdate = "UPDATE tblLogin SET senhaLog = '" & senha & "', emailUsuario = '" & email & "' WHERE codLogin = " & Login.usuario.Codigo
                    Try
                        cmd.CommandText = strupdate
                        cmd.ExecuteNonQuery()
                        KryptonMessageBox.Show("Salvo com sucesso!", "Salvo")
                        txtconfirmsenha.Clear()
                        txtsenha.Enabled = False
                        txtconfirmsenha.Enabled = False
                        txtemail.Enabled = False
                        lblsenhat.Text = "Senha:"
                        lblconfirmt.Text = "Confirme a senha:"
                        lblemailt.Text = "Email:"
                        lblaviso.Visible = False
                    Catch ex As Exception
                        Debug.WriteLine(ex)
                    End Try
                Else
                    txtemail.Clear()
                    KryptonMessageBox.Show("Digite um email válido!", "Atenção")

                End If


            Else
                KryptonMessageBox.Show("A senha de CONFIRMAÇÃO deve ser igual a nova senha!", "Atenção")
            End If
        End If


    End Sub

    Private Sub btnsalvar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsalvar.Click
        atualizausuario()

    End Sub

End Class